/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_ssap_client_hdi.h"
#include "sle_ssap_config.h"
#include "sle_ssap_client_config.h"
#include "sle_hal_errcode.h"
#include "sle_unit_test_log.h"

static SleSsapcHdiCallbacks *g_ssapcHdiTestCbs;
static uint8_t g_clientId = 0;
static SleUuid g_serverUuid = {
    .len = 16,
};

SleHdiErrorCode SleHdiSsapcRegisterClientTest(SleUuid *appUuid, uint8_t *clientId)
{
    *clientId = g_clientId;
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode SleHdiSsapcUnregisterClientTest(uint8_t clientId)
{
    g_clientId = 65535;
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode SleHdiSsapcFindStructureTest(uint8_t clientId, uint16_t connectId, ClientFindStructure *param)
{
    return SLE_HDI_ERRCODE_SUCC;
    ClientFindStructureRes res = {
        .type = param->findType,
        .uuid = g_serverUuid,
    };
    ClientFindPropertyRes propertyRes = {
        .handle = 0,
        .operateIndication = SSAP_OPERATEIND_BIT_WRITE_NO_RSP,
        .uuid = g_serverUuid,
        .descriptorsCount = 1,
        .descriptorsType = SSAP_PROPERTY_DESCRIPTOR_TYPE_UNKNOWN,
    };
    if (g_ssapcHdiTestCbs != NULL) {
        g_ssapcHdiTestCbs->OnFindStructureCmpCb(clientId, connectId, &res, SLE_HDI_ERRCODE_SUCC);
        g_ssapcHdiTestCbs->OnPropertyFoundCb(clientId, connectId, &propertyRes, SLE_HDI_ERRCODE_SUCC);
    }
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode SleHdiSsapcReadReqByUuidTest(uint8_t clientId, uint16_t connectId, ClientReadReqByUUID *param)
{
    uint8_t data[10] = {'s','l','e','_','t','e','s','t','0','\0'};
    ClientHandleValue readData = {
        .handle = 1,
        .type = SLE_SSAP_PROPERTY_TYPE,
        .dataLength = 10,
        .data = data,
    };
    ClientReadByUuidCmpRes redByUuidRes = {
        .type = SLE_SSAP_PROPERTY_TYPE,
        .uuid = g_serverUuid,
    };
    if (g_ssapcHdiTestCbs != NULL) {
        g_ssapcHdiTestCbs->OnRreadByUuidCmpCb(clientId, connectId, &redByUuidRes, SLE_HDI_ERRCODE_SUCC);
        g_ssapcHdiTestCbs->OnRecvReadResponseCb(clientId, connectId, &readData, SLE_HDI_ERRCODE_SUCC);
    }
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode SleHdiSsapcReadReqTest(uint8_t clientId, uint16_t connectId, uint16_t handle, uint8_t type)
{
    uint8_t data[10] = {'s','l','e','_','t','e','s','t','0','\0'};
    ClientHandleValue readData = {
        .handle = 1,
        .type = SLE_SSAP_PROPERTY_TYPE,
        .dataLength = 10,
        .data = data,
    };
    if (g_ssapcHdiTestCbs != NULL) {
        g_ssapcHdiTestCbs->OnRecvReadResponseCb(clientId, connectId, &readData, SLE_HDI_ERRCODE_SUCC);
    }
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode SleHdiSsapcWriteReqTest(uint8_t clientId, uint16_t connectId, ClientWriteParam *param)
{
    ClientWriteRes writeRes = {
        .handle = param->handle,
        .type = SLE_SSAP_PROPERTY_TYPE,
    };
    if (g_ssapcHdiTestCbs != NULL) {
        g_ssapcHdiTestCbs->OnRecvWriteResponseCb(clientId, connectId, &writeRes, SLE_HDI_ERRCODE_SUCC);
    }
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode SleHdiSsapcWriteCmdTest(uint8_t clientId, uint16_t connectId, ClientWriteParam *param)
{
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode SleHdiSsapcExchangeInfoReqTest(uint8_t clientId, uint16_t connectId, SsapMtuInfo* param)
{
    if (g_ssapcHdiTestCbs != NULL) {
        g_ssapcHdiTestCbs->OnExchangeInfoCb(clientId, connectId, param, SLE_HDI_ERRCODE_SUCC);
    }
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode SleHdiRegisterSsapcCallbacksTest(SleSsapcHdiCallbacks *ssapcCallbacks)
{
    SLE_UT_CHECK_AND_RETURN_RET_LOG(ssapcCallbacks != NULL, SLE_HAL_ERR_INVALID_PARAM, "SleHdiRegisterSsapcCallbacksTest");
    g_ssapcHdiTestCbs = ssapcCallbacks;
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode GetSleSsapcHdiOpts(SleSsapcHdiOpts *ssapcOpts)
{
    SLE_UT_CHECK_AND_RETURN_RET_LOG(ssapcOpts != NULL, SLE_HAL_ERR_INVALID_PARAM, "GetSleSsapcHdiOpts");
    uint8_t uuid[UUID_LEN] = {1};
    memcpy(g_serverUuid.uuid, uuid, sizeof(uuid));
    ssapcOpts->registerClient = SleHdiSsapcRegisterClientTest;
    ssapcOpts->unregisterClient = SleHdiSsapcUnregisterClientTest;
    ssapcOpts->findStructure = SleHdiSsapcFindStructureTest;
    ssapcOpts->readReqByUuid = SleHdiSsapcReadReqByUuidTest;
    ssapcOpts->readReq = SleHdiSsapcReadReqTest;
    ssapcOpts->writeReq = SleHdiSsapcWriteReqTest;
    ssapcOpts->writeCmd = SleHdiSsapcWriteCmdTest;
    ssapcOpts->exchangeInfoReq = SleHdiSsapcExchangeInfoReqTest;
    ssapcOpts->registerClientCbks = SleHdiRegisterSsapcCallbacksTest;
    return SLE_HDI_ERRCODE_SUCC;
}